package com.ywwxhz.Util;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.widget.Toast;

import java.util.Locale;

public class ToolKit {

    public static void MakeToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void MakeToast(Context context, int resID) {
        Toast.makeText(context, resID, Toast.LENGTH_SHORT).show();
    }

    public static void Log(String TAG, String msg) {
        if (Config.DEBUG) {
            android.util.Log.d(TAG, msg);
        } else {
            android.util.Log.v(TAG, msg);
        }
    }

    public static String getFixIgnoreList() {
        return Config.fixignore;
    }

    // 获得系统可用内存信息
    public static String getAvaliableMemory(Context context, ActivityManager am) {
        // 获得MemoryInfo对象
        MemoryInfo memoryInfo = new MemoryInfo();
        // 获得系统可用内存，保存在MemoryInfo对象上
        am.getMemoryInfo(memoryInfo);
        long memSize = memoryInfo.availMem;

        return FileKit.formateFileSize(context, memSize);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }

    public static void Kill_Proc(String pkgname, ActivityManager am) {
        am.killBackgroundProcesses(pkgname);
    }

    public static void addToIgnoreList(Context context, String pkgname) {
        SharedPreferences appPrefs = PreferencesKit.getWriteableSharedPreferences(context);
        StringBuffer sb = new StringBuffer(appPrefs.getString("ignorelist", ""));
        SharedPreferences.Editor prefsEditor = appPrefs.edit();
        sb.append(",");
        sb.append(pkgname);
        prefsEditor.putString("ignorelist", sb.toString());
        prefsEditor.commit();
    }

    public static String getIgnoreList(Context context) {
        SharedPreferences appPrefs = PreferencesKit.getReadableSharedPreferences(context);
        StringBuffer sb = new StringBuffer();
        sb.append(appPrefs.getString("fixignore", ""));
        sb.append(",");
        sb.append(appPrefs.getString("ignorelist", ""));
        return sb.toString();
    }

    public static void firstrun(Context context) {
        SharedPreferences appPrefs = PreferencesKit.getWriteableSharedPreferences(context);
        if (appPrefs.getBoolean("firstrun", true)) {
            SharedPreferences.Editor prefsEditor = appPrefs.edit();
            prefsEditor.putBoolean("firstrun", false);
            prefsEditor.putString("widgetFlushRate", "3000");
            prefsEditor.putString("autokillFlushRate", "600000");
            prefsEditor.putString("fixignore", Config.fixignore);
            prefsEditor.commit();
        }
    }

    public static String FormatTime(long timespan) {
        String time;
        long between = timespan / 1000;// 除以1000是为了转换成秒
        long day1 = between / (24 * 3600);
        long hour1 = between % (24 * 3600) / 3600;
        long minute1 = between % 3600 / 60;
        time = String.format(Locale.CHINA, "%d %s:%s", day1, hour1 < 10 ? "0"
                + hour1 : "" + hour1, minute1 < 10 ? "0" + minute1 : ""
                + minute1);
        return time;
    }

    /*
     * 判断是否是平板
     *
     * @param context
     *
     * @return
     */
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

}
